#version 460
#extension GL_GOOGLE_include_directive : enable
#extension GL_EXT_scalar_block_layout : enable
#extension GL_EXT_buffer_reference2 : require
#extension GL_EXT_shader_explicit_arithmetic_types_int64 : require


layout(points) in;
layout(line_strip, max_vertices=96) out;


#include "../../../../core/shader_color.h"
#include "../../../../core/shader_curve.h"
#include "../../../../core/shader_taster_curve.h"



layout(location = 1) in VS_GS_VERTEX {
	uint vInstance;
}vertex_in[];


layout(location = 0) out vec4 gColor;
out gl_PerVertex {
	vec4	gl_Position;
	//float	gl_PointSize;
};



//vec4 v = vec4(0, 0, 0, 1);

void main(void){
	uint Instance = vertex_in[0].vInstance;
	//int num = 16;

	S_CurvePoint s_curve, e_curve;
	//vec2 s_point, e_point, L_dir, R_dir, V_dir;
	//float l_len, r_len, v_len;
	vec4 Color[2];

	uint s_ID = Instance;
	uint e_ID = Instance + 1;

	s_curve = m_Curve[s_ID];
	e_curve = m_Curve[e_ID];



	Color[0] = f_glsl_buildColor(s_curve.ID_Color.y);
	Color[1] = f_glsl_buildColor(e_curve.ID_Color.y);

	//f_curve_point_control(Instance, s_point, e_point, L_dir, R_dir, V_dir, l_len, r_len, v_len, s_curve, e_curve, Color, num);
	//if(m_GradientNum < 2) {
	//	EndPrimitive();
	//	return;
	//}

	for(uint i = 0; i <= 32; ++i){
		vec4 vert = vec4(f_curve_cal_point(s_curve, e_curve, (i*0.03125)), 0, 1);

		//vec2 p0 = m_GradientNum > 2 ? m_Curve[s_ID].value : m_Curve[0].value;
        //vec2 p1 = m_Curve[s_ID].value;
        //vec2 p2 = m_Curve[s_ID + 1].value;
        //vec2 p3 = m_GradientNum > 2 ? m_Curve[s_ID + 2].value : m_Curve[s_ID + 1].value;
		////vec4 vert = vec4( f_glsl_cal_CatmullRom3(vec3(p0, 0), vec3(p1, 0), vec3(p2, 0), vec3(p3, 0), (i*0.03125)), 1);
		//vec4 vert = vec4( f_glsl_cal_CubicInterpolate2(p0, p1, p2, p3, (i*0.03125)), 0, 1);


		vert.xy *= m_Scale;
		vert.xy += m_Loc;
		
		gl_Position = mat_ProjecView * vert;

		gColor = Color[0];
		//gColor.rgb += vert.y / m_Scale.y;
		EmitVertex();
	}
	EndPrimitive();
	
	
	if(Instance == 0){
		S_CurvePoint curve_0 = s_curve;
		curve_0.value = s_curve.L_point;

		for(uint i = 0; i <= 32; ++i){
			vec4 vert = vec4(f_curve_cal_point(curve_0, s_curve, (i*0.03125)), 0, 1);
			vert.xy *= m_Scale;
			vert.xy += m_Loc;
			
			gl_Position = mat_ProjecView * vert;

			gColor = Color[0];
			//gColor.rgb += vert.y / m_Scale.y;
			EmitVertex();
		}
		EndPrimitive();
	} else if (Instance >= m_GradientNum) {
		S_CurvePoint curve_next = e_curve;
		curve_next.value = e_curve.R_point;

		for(uint i = 0; i <= 32; ++i){
			vec4 vert = vec4(f_curve_cal_point(e_curve, curve_next, (i*0.03125)), 0, 1);
			vert.xy *= m_Scale;
			vert.xy += m_Loc;
			
			gl_Position = mat_ProjecView * vert;

			gColor = Color[0];
			//gColor.rgb += vert.y / m_Scale.y;
			EmitVertex();
		}
		EndPrimitive();
	}


}



 